{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data Types and Type Conversion\n",
    "\n",
    "**Time**\n",
    "- Teaching: 5 min\n",
    "- Challenges: 15 min\n",
    "\n",
    "**Questions**\n",
    "- \"What kinds of data do programs store?\"\n",
    "- \"How can I convert one [type](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#type) to another?\"\n",
    "\n",
    "**Learning Objectives**\n",
    "- \"Explain key differences between [integers](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#integer) and [floating point numbers](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#floating-point-number).\"\n",
    "- \"Explain key differences between numbers and [character strings](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#string).\"\n",
    "- \"Use built-in functions to convert between integers, floating point numbers, and strings.\"\n",
    "* * * * *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Every value has a type.\n",
    "\n",
    "*   Every value in a program has a specific type.\n",
    "*   Integer (`int`): counting numbers like 3 or -512.\n",
    "*   Floating point number (`float`): fractional numbers like 3.14159 or -2.5.\n",
    "    *   Integers are used to count, floats are used to measure.\n",
    "*   Character string (usually just called \"string\", `str`): text.\n",
    "    *   Written in either single quotes or double quotes (as long as they match).\n",
    "    *   The quotation marks aren't printed when the string is displayed.\n",
    "\n",
    "## Use the built-in function `type` to find the type of a value.\n",
    "\n",
    "*   Use the built-in function `type` to find out what type a value has.\n",
    "*   Works on variables as well.\n",
    "    *   But remember: the *value* has the type --- the *variable* is just a label.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(type(52))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pi = 3.14159\n",
    "print(type(pi))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fitness = 'average'\n",
    "print(type(fitness))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Types control what operations can be done on values.\n",
    "\n",
    "*   A value's type determines what the program can do to it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(5 - 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('hello' - 'h')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Strings can be added and multiplied.\n",
    "\n",
    "*   \"Adding\" character strings concatenates them.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_name = 'Ahmed' + ' ' + 'Walsh'\n",
    "print(full_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*   Multiplying a character string by an [integer](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#integer) replicates it.\n",
    "    *   Since multiplication is just repeated addition.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "separator = '=' * 10\n",
    "print(separator)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Strings have a length (but numbers don't).\n",
    "\n",
    "*   The built-in function `len` counts the number of characters in a string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(full_name))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*   But numbers don't have a length (not even zero)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(52))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Must convert numbers to strings or vice versa when operating on them.\n",
    "\n",
    "*   Cannot add numbers and strings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(1 + '2')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*   Not allowed because it's ambiguous: should `1 + '2'` be `3` or `'12'`?\n",
    "*   Use the name of a type as a function to convert a value to that type.\n",
    "*   This conversion is also called *casting* a value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(1 + int('2'))\n",
    "print(str(1) + '2')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Can mix integers and floats freely in operations.\n",
    "\n",
    "*   Integers and floating-point numbers can be mixed in arithmetic.\n",
    "    *   Python automatically converts integers to floats as needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('half is', 1 / 2.0)\n",
    "print('three squared is', 3.0 ** 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Variables only change value when something is assigned to them.\n",
    "\n",
    "*   If we make one cell in a spreadsheet depend on another,\n",
    "    and update the latter,\n",
    "    the former updates automatically.\n",
    "*   This does *not* happen in programming languages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first = 1\n",
    "second = 5 * first\n",
    "first = 2\n",
    "print('first is', first, 'and second is', second)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*   The computer reads the value of `first` when doing the multiplication,\n",
    "    creates a new value, and assigns it to `second`.\n",
    "*   After that, `second` does not remember where it came from."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*   The computer reads the value of `first` when doing the multiplication,\n",
    "    creates a new value, and assigns it to `second`.\n",
    "*   After that, `second` does not remember where it came from."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 1: Making and Casting Variables\n",
    "\n",
    "1. Make a variable `year` and [assign](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#assign) it as the year you were born\n",
    "2. Cast that variable to a float, and assign it to a new variable `year_float`\n",
    "3. Cast `year_float` to a string, and assign it to a new variable `year_string`\n",
    "4. Someone in your class says they were born in 1997. Find out what your age difference is, using only `year_string`.\n",
    "\n",
    "Some of the steps in this challenge might seem redundant or unnecessary, and they are. The idea is just to give you practice with coercing variables, even if it's unlikely you'll ever need to do it so many times in a row."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 2: Division Types\n",
    "\n",
    "The `//` operator calculates the whole-number result of division, while the '%' operator calculates the remainder from division. Run the cell below to see an example of each."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('5 // 3:', 5//3)\n",
    "print('5 % 3:', 5%3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You are ordering pizza for a campus event.\n",
    "\n",
    "You have ordered 8 pizzas, with 8 slices each, for a total of 64 slices.\n",
    "\n",
    "You know that everyone attending will eat exactly 3 slices of pizza. You want to make sure that everyone who comes gets enough pizza, even if that means a slice or two will be left over.\n",
    "\n",
    "Write an expression that calculates how many students you can satisfactorily feed with 64 slices of pizza, provided that each will eat 3 slices."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "slices = 64\n",
    "slices_per_student = 3\n",
    "\n",
    "# YOUR CODE HERE\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 3: Strings to Numbers\n",
    "\n",
    " `float` will convert a string to a floating point number, and `int` will convert a floating point number to an integer:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"string to float:\", float(\"3.4\"))\n",
    "print(\"float to int:\", int(3.4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Given that, what do you expect this program to do? What does it actually do? Why do you think it does that?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"fractional string to int:\", int(\"3.4\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 4: Arithmetic with Different Types\n",
    "\n",
    "Which of the following will print 2.0?\n",
    "\n",
    "Note: there may be more than one right answer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first = 1.0\n",
    "second = \"1\"\n",
    "third = \"1.1\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. `first + float(second)`\n",
    "2. `float(second) + float(third)`\n",
    "3. `first + int(third)`\n",
    "4. `first + int(float(third))`\n",
    "5. `int(first) + int(float(third))`\n",
    "6. `2.0 * second`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Does `first == second` ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Change the code below to yield `True` by casting the variable(s)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first == second"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 5: More Number Types"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Predict the output of the following:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(8/4)\n",
    "type(8/4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(8/5)\n",
    "type(8/5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(8*4)\n",
    "type(8*4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(8*4.5)\n",
    "type(8*4.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Change the following code to make the output `True`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "num1 = 30\n",
    "num2 = 35.0\n",
    "\n",
    "type(num1) == type(num2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 6: String Syntax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Why does the following code produce an error?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Hello World!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And this one?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"I am \" + 27 + \" years old.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Fix the above cells."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*****\n",
    "\n",
    "# Keypoints\n",
    "\n",
    "- \"Every value has a type.\"\n",
    "- \"Use the built-in function `type` to find the type of a value.\"\n",
    "- \"Types control what operations can be done on values.\"\n",
    "- \"Strings can be added and multiplied.\"\n",
    "- \"Strings have a length (but numbers don't).\"\n",
    "- \"Must convert numbers to strings or vice versa when operating on them.\"\n",
    "- \"Can mix integers and floats freely in operations.\"\n",
    "- \"Variables only change value when something is assigned to them.\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
